<?php

class Admin extends App_Abstract {

	public $module;

	public function run () {
		$module = Request_Get::get('module');
		if (!$module) $module = self::inst()->config['default_module'];
		if (!$module) Request::redirect(404);

		$action = Request_Get::get('action');
		if (!$action) $action = "index";

		$this->module = Modules_Admin_Abstract::module($module);
		$this->module->action = $action;

		$content = $this->module->render();
		$modules = Db::model("Modules")->findAll(array(
			'with' => 'Modules.Items',
			'where' => "parent = 0",
		));
				
		$this->tpl()->assign(array(
			'content' => $content,
			'title' => App::inst()->config['admin_title'],
			'menu' => $modules,
		));

		echo $this->tpl()->fetch('index.html');
	}

	public static function create ($configFile, $className = __CLASS__) {
		return parent::create($configFile, $className);
	}

	public static function inst ($className = __CLASS__) {
		return parent::inst($className);
	}

	public function tpl () {
		if ($this->_tpl === null) $this->_tpl = new Tpl_Admin ();
		return $this->_tpl;
	}

}